if (localStorage.getItem('users')) {

    var usersArr = JSON.parse(localStorage.getItem('users'))
} else {
    var usersArr = []
}
//通过ajax获取json数据
$.ajax({
    url: '../data/data5.json',
    type: 'get',
    datatype: 'json',
    success: function (data) {
        var flag = false
        //本地存储没有该数据则进行存储
        $.each(data, function (index, item) {
            $.each(usersArr, function (index, itm) {
                if (item.username === itm.username) {
                    flag = true
                }
            })
            if (!flag) {
                usersArr.push({ 'username': item.username, 'password': item.password, 'name': item.name })
            }
            localStorage.setItem('users', JSON.stringify(usersArr))
        })
    }
})
//为提交按钮绑定点击事件
$('.tijiao').on('click', function () {

    var username = $('.number').val()
    var password = $('.password').val()
    var flag = false
    $.each(usersArr, function (ind, item) {

        if (item.username === username && item.password === password) {
            var nameArr = []
            nameArr.push({ 'name': item.name })
            localStorage.setItem('name', JSON.stringify(nameArr))
            var urlArr = JSON.parse(localStorage.getItem('url'))
            $.each(urlArr,function(index,item){
                
                 window.location.href = item.url
            })
           
            window.event.returnValue = false;
            alert("登录成功！")

        }


    })
    if (!flag) {
        $.each(usersArr, function (ind, item) {
            if (item.username != username && item.password != password) {
                alert("账号或密码错误！")
                return false
            }
        })
    }


})
//改变密码输入框是否可见
$('.shulu').on('click', 'span', function () {

    var type1 = this.previousElementSibling.type
    console.log(type1)
    if (type1 === "password") {
        this.previousElementSibling.type = "text"
        $(this).children('img').attr('src', '../imgs/yj2.png')
        console.log(1)
    } else {
        this.previousElementSibling.type = "password"
        $(this).children('img').attr('src', '../imgs/yj1.png')
    }
})

